package com.noble.meether.mail.ui.friend.request;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.ExBaseAdapter;
import com.noble.library.image.ImageLoader;
import com.noble.library.view.custom.state.CustomFrameLayout;
import com.noble.meether.R;

import java.util.List;

public class MyFriendRequestAdapter extends ExBaseAdapter<UserLight> {

    public MyFriendRequestAdapter(Context context, List<UserLight> data) {
        super(context, data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.item_friend_request);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        final UserLight userInfo = getItem(position);
        ImageLoader.loadCircleAvatar(getContext(), userInfo.getAvatar(), holder.iv_avatar);
        holder.tv_name.setText(userInfo.getNickname());
        holder.user_vip.setVisibility(userInfo.isVip() ? View.VISIBLE : View.GONE);

        // 对方已拒绝好友申请
        if (userInfo.getStatusFriend() == -3) {
            showIndex1(holder.customLayout, holder.btn_addblack, userInfo.getUid());
        } else {
            holder.customLayout.showOfIndex(0);
            holder.btn_reject.setOnClickListener(view -> ModuleMgr.getCommonMgr().setRefusedFriends(userInfo.getUid(), response -> {
                if (response.isOk())
                    showIndex1(holder.customLayout, holder.btn_addblack, userInfo.getUid());
            }));
            holder.btn_agree.setOnClickListener(view -> ModuleMgr.getCommonMgr().setMyFriends(userInfo.getUid(), null));
        }
        return convertView;
    }

    private void showIndex1(CustomFrameLayout customLayout, TextView addBlack, final long uid) {
        customLayout.showOfIndex(1);
        addBlack.setOnClickListener(view -> ModuleMgr.getCommonMgr().setBlackList(uid, null));
    }

    private class MyViewHolder {
        private CustomFrameLayout customLayout;
        private ImageView iv_avatar, user_vip;
        private TextView tv_name, btn_agree, btn_reject, btn_addblack;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            customLayout = convertView.findViewById(R.id.customLayout);
            iv_avatar = convertView.findViewById(R.id.icon_friend);
            tv_name = convertView.findViewById(R.id.user_nick);
            btn_reject = convertView.findViewById(R.id.btn_reject);
            btn_agree = convertView.findViewById(R.id.btn_agree);
            btn_addblack = convertView.findViewById(R.id.btn_addblack);
            user_vip = convertView.findViewById(R.id.user_vip);
        }
    }
}
